package hello;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;


@RestController
public class ResourceController {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/wallpaper";
    static final String USER = "root";
    static final String PASS = "88888888";

    @RequestMapping(value = "/downloads")
    public String downloads(@RequestParam(value="name", defaultValue="World") String name,
                       @RequestParam(value="id", defaultValue="1") String id) {

        for(int i = 0; i < GreetingController.maxuser; ++i){
            if(GreetingController.logined[0].equals(name)){
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn = null;
                    Statement stmt = null;
                    conn = DriverManager.getConnection(DB_URL, USER, PASS);

                    stmt = conn.createStatement();
                    String sql;
                    sql = "SELECT url FROM wallpapers WHERE id=\"%d\"";
                    ResultSet rs = stmt.executeQuery(String.format(sql, Integer.valueOf(id).intValue()));
                    if(rs.next()){
                        return rs.getString("url");
                    }
                    else{
                        return "no such resource";
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return "login first please";
    }


    @RequestMapping(value = "/getall")
    public String[] downloads(@RequestParam(value="name", defaultValue="World") String name) {

        for(int i = 0; i < GreetingController.maxuser; ++i){
            if(GreetingController.logined[0].equals(name)){
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn = null;
                    Statement stmt = null;
                    conn = DriverManager.getConnection(DB_URL, USER, PASS);

                    stmt = conn.createStatement();
                    String sql;
                    sql = "SELECT url FROM wallpapers";
                    ResultSet rs = stmt.executeQuery(sql);
                    if(rs.next()){
                        rs.last();
                        String []result = new String[rs.getRow()];
                        rs.first();
                        int a=0;
                        do{
                            result[a] = rs.getString("url");
                            ++a;
                        }while(rs.next());
                        return result;
                    }
                    else{
                        String []result = new String[1];
                        result[0] = "no such resource";
                        return result;
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        String []result = new String[1];
        result[0] = "login first please";
        return result;
    }

    @RequestMapping(value = "/search")
    public String[] search(@RequestParam(value="name", defaultValue="World") String name,
                            @RequestParam(value="search", defaultValue="1") String search){
        for(int i = 0; i < GreetingController.maxuser; ++i){
            if(GreetingController.logined[0].equals(name)){
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection conn = null;
                    Statement stmt = null;
                    conn = DriverManager.getConnection(DB_URL, USER, PASS);

                    stmt = conn.createStatement();
                    String sql;
                    sql = "SELECT url FROM wallpapers WHERE description like \"%s\"";
                    ResultSet rs = stmt.executeQuery(String.format(sql, "%"+search+"%"));
                    String[] result;
                    if(rs.next()){
                        rs.last();
                        result = new String[rs.getRow()];
                        rs.first();
                        int a=0;
                        do {
                            result[a] = rs.getString("url");
                            ++a;
                        }while(rs.next());
                        return result;
                    }
                    else{
                        result = new String[1];
                        result[0] = "no such resource";
                        return result;
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();

                    String []result = new String[1];
                    result[0] = "server met some exceptions";
                    return result;
                }
            }
        }
        String []result = new String[1];
        result[0] = "login first please";
        return result;
    }

}
